package cn.itcast.service.impl;

import cn.itcast.dao.MenuDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.pojo.Menu;
import cn.itcast.pojo.Permission;
import cn.itcast.service.MenuService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private  MenuDao menuDao;
    @Override
    public List<Menu> selectAll() {
        return menuDao.selectAll();
    }

    /**
     * 根据id查询菜单信息
     * @param id
     * @return
     */
    @Override
    public Menu selectMenuById(Integer id) {
        return menuDao.selectMenuById(id);
    }

    /**
     *分页查询
     * @param queryPageBean
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
       //开启分页
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //条件查询
        Page<Menu> menus = this.menuDao.selectByCondition(queryPageBean);
        Page<Menu> list = new Page<>();
        for (Menu menu : menus) {
            if (menu.getParentMenuId() == 0) {
                Menu menu1 = new Menu();
                List<Menu> menusC = this.menuDao.selectChild(menu.getId());
                menu1.setChildren(menusC);
                menu1.setId(menu.getId());
                menu1.setDescription(menu.getDescription());
                menu1.setIcon(menu.getIcon());
                menu1.setLevel(menu.getLevel());
                menu1.setLinkUrl(menu.getLinkUrl());
                menu1.setName(menu.getName());
                menu1.setPriority(menu.getPriority());
                list.add(menu1);
            }
        }

        return  new PageResult(menus.getTotal(),list.getResult());
    }

    /**
     * 查询子父类
     * @return
     */
    @Override
    public List<Menu> findParentChild() {

        List<Menu> menusP = this.menuDao.selectParent();
        //List<Menu> menusC = this.menuDao.selectChild();
        List<Menu> menuFinal = new ArrayList<>();
        for (Menu menu : menusP) {
            Menu menu1 = new Menu();
            List<Menu> menusC = this.menuDao.selectChild(menu.getId());
            menu1.setChildren(menusC);
            menu1.setId(menu.getId());
            menu1.setDescription(menu.getDescription());
            menu1.setIcon(menu.getIcon());
            menu1.setLevel(menu.getLevel());
/*
            menu1.setLinkUrl("localhost:9000/"+menu.getLinkUrl());
*/
            menu1.setLinkUrl("https://www.baidu.com");
            menu1.setName(menu.getName());
            menu1.setPriority(menu.getPriority());
            menuFinal.add(menu1);
        }
                return menuFinal;
        }

    @Transactional
    @Override
    public void handleAdd(Menu menu, Integer[] permissionIds) {
        //保存菜单信息
        menuDao.add(menu);
        //关联保存检查想信息
        if (permissionIds !=null){
            Integer menuId = menu.getId();
            setMenuAndPermission(menuId,permissionIds);
        }
    }

    /**
     * 设置菜单和权限的关联信息
     */
    private void setMenuAndPermission(Integer menuId,Integer[] permissionIds){
        Stream.of(permissionIds).forEach(permissionId -> menuDao.setPermissionsAssociation(menuId, permissionId));
    }

    /**
     * 删除菜单
     * @param id
     */
    @Override
    @Transactional
    public void deleteById(Integer id) {
        //删除关联信息
        menuDao.deletePermissionAssociation(id);
        //删除菜单信息
        menuDao.deleteById(id);
    }

    /**
     * 更新
     * @param menu
     * @param permissionIds
     */
    @Transactional
    @Override
    public void update(Menu menu, Integer[] permissionIds) {
        //更新菜单基本信息
        menuDao.update(menu);
        //删除菜单相关联的权限信息
        menuDao.deletePermissionAssociation(menu.getId());
        //设置菜单相关联的权限信息
        setMenuAndPermission(menu.getId(),permissionIds);
    }

    /**
     * 根据id查询相关的permission信息
     * @param id
     * @return
     */
    @Override
    public Integer[]  selectPermissionById(Integer id) {
        //根据id查询
        Integer[] ids = menuDao.selectPermissionById(id);
        return ids;
    }
}

